<?php
namespace app\session\controller;

use tp5_lib\api\controller\ApiBaseController;
use think\facade\Session;

class ApiController extends ApiBaseController
{
    protected $prefix="main";
    protected $key="test";
    protected $value="hello world !";

    protected function initSession()
    {
        if(!$token=input("token")){
            $this->error("token不能为空");
        }
        Session::init(["id"=>$token]);
    }
    public function token()
    {
        $this->result(["token"=>Session::id()],1);
    }
    public function get()
    {
        $this->initSession();
        $this->result([
            "id"=>Session::id(),
            "value"=>session($this->key),
            "prefix_value"=>session($this->key,'',$this->prefix),
        ],1);
    }
    public function set()
    {
        $this->initSession();
        session($this->key,$this->value);
        session($this->key,$this->value,$this->prefix);
        $this->success();
    }
    public function destroy()
    {
        $this->initSession();
        session($this->key,null);
        session($this->key,null,$this->prefix);
        $this->success();
    }
    public function transfer()
    {
        $this->initSession();
        $id=Session::transfer();
        $this->result(["token"=>$id],1);
    }
}
